package handler

import (
	"Gin/api/request"
	"Gin/global/model"
	"Gin/global/pkg"
	"Serve/VideoSrv"
	"context"
	"github.com/gin-gonic/gin"
	"net/http"
)

func VidepUpdate(c *gin.Context) {
	var VideoUpdateRequest request.VideoUpdateRequest
	if err := c.ShouldBind(&VideoUpdateRequest); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    http.StatusBadRequest,
			"message": err.Error(),
		})
		return
	}
	var image_url string
	file, m, err := c.Request.FormFile("image")
	if err == nil {
		upload, err := pkg.Upload(file, m)
		if err != nil {
			c.JSON(http.StatusBadRequest, gin.H{
				"code":    http.StatusBadRequest,
				"message": err.Error(),
			})
			return
		}
		image_url = upload
	} else {
		image_url = ""
	}
	update, err := model.VideoClient.VideoUpdate(context.Background(), &VideoSrv.VideoUpdateRequest{
		Title:    VideoUpdateRequest.Title,
		Class:    int64(VideoUpdateRequest.Class),
		Des:      VideoUpdateRequest.Des,
		Tag:      VideoUpdateRequest.Tag,
		VideoId:  int64(VideoUpdateRequest.VideoId),
		ImageUrl: image_url,
		UserId:   int64(VideoUpdateRequest.UserId),
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    http.StatusBadRequest,
			"message": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "OK",
		"data":    update,
	})
}
